package com.jadcon.harveycedars.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.harveycedars.model.dormitory.Gender;
import com.jadcon.harveycedars.model.dormitory.Person;

@Controller
public class TestFormController {

	@RequestMapping("/editPerson.html")
	public ModelAndView showEditPersonForm() {
		return new ModelAndView("editPerson");
	}
	
	/**
	 * This is the most basic way of mapping HTTP request
	 * parameters (name=value) to your Objects.  It is not
	 * generally used in practice with Spring MVC because
	 * the other ways (shown below) are less work.
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/savePerson1.html")
	public ModelAndView savePerson1(HttpServletRequest request) {
		
		String fullname = request.getParameter("fullname");
		String gender = request.getParameter("gender");
		String age = request.getParameter("age");
		String churchname = request.getParameter("churchname");
		
		Person person = new Person();
		person.setFullname(fullname);
		person.setGender(Gender.valueOf(gender));
		person.setAge(Integer.parseInt(age));
		person.setChurchname(churchname);
		
		System.out.println("TestFormController.savePerson1(): " + person);
		
		return showEditPersonForm();
	}

	/**
	 * This is a way to map request parameters to method parameters.
	 * Notice how Spring MVC does the type conversion for you, so
	 * gender is a Gender and age is an int.  This way is commonly
	 * used if you have a couple of request parameters, but not usually
	 * for form submission.
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/savePerson2.html")
	public ModelAndView savePerson2(@RequestParam String fullname,
									@RequestParam Gender gender,
									@RequestParam int age,
									@RequestParam String churchname) {
		
		Person person = new Person();
		person.setFullname(fullname);
		person.setGender(gender);
		person.setAge(age);
		person.setChurchname(churchname);
		
		System.out.println("TestFormController.savePerson2(): " + person);
		
		return showEditPersonForm();
	}
	
	/**
	 * This is the preferred way to handle form submissions.  Spring MVC
	 * takes each request parameter (like 'fullname=bob') and looks for
	 * a setter method (like setFullname) on the Object you use as a
	 * method parameter which is a Person in this case.
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/savePerson3.html")
	public ModelAndView savePerson3(Person person) {
		
		System.out.println("TestFormController.savePerson3(): " + person);
		
		return showEditPersonForm();
	}
	


}
